package com.xii.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xii.constant.MessageConstant;
import com.xii.entity.PageResult;
import com.xii.entity.QueryPageBean;
import com.xii.entity.Result;
import com.xii.pojo.CheckGroup;
import com.xii.pojo.Setmeal;
import com.xii.service.CheckGroupService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;


    /**
     *  新增检查组
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.add(checkitemIds,checkGroup);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @PostMapping("findPage")
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        return  checkGroupService.findPage(pageBean);
    }


    /**
     * 查询检查组
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    @ResponseBody
    public Result findById(Integer id){

        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }


    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    @ResponseBody
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try {
            List<Integer> checkGroupitemlist = checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupitemlist);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }


    /**
     * @description: 编辑修改
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
     **/
    @PostMapping("/edit")
    @ResponseBody
    public Result edit(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup){
        try {
            checkGroupService.edit(checkitemIds,checkGroup);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }
    }


    @GetMapping("findAll")
    @ResponseBody
    public Result findAll(){
        try {
            List<Setmeal> setmealList = checkGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,setmealList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
